import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * @Author：LJ
 * @Description：
 * @Date: 2017/11/29
 * @Modified By:
 */
public class TestMain {
    public static void main(String[] args) {
        method3();
    }

    private static void method4(){
        try {
            Class tem= Class.forName("TestMain",true,Thread.currentThread().getContextClassLoader());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static void method3(){
        System.out.println("当前线程上下文："+Thread.currentThread().getContextClassLoader());
        ClassLoader loader = TestMain.class.getClassLoader();
        while (loader != null) {
            System.out.println(loader.toString());
            loader = loader.getParent();
        }
    }

    public static void method2() {
        try {
            URL url = new URL("file:F:/TestAction.jar");
            URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url}, Thread.currentThread().getContextClassLoader());
            URLClassLoader urlClassLoader1 = new URLClassLoader(new URL[]{url}, Thread.currentThread().getContextClassLoader());
            Class myClass = urlClassLoader.loadClass("Sample");
            System.out.println(myClass.hashCode());
            Object object=myClass.newInstance();
            Class myClass1 = urlClassLoader.loadClass("Sample");
            System.out.println(myClass1.hashCode());
            Object object1=myClass1.newInstance();
            Method method=myClass.getMethod("setInstance",Object.class);
            method.invoke(object,object1);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

    }

    private static void method1() {
        try {
            URL url = new URL("file:F:/TestAction.jar");
            URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url}, Thread.currentThread().getContextClassLoader());
            Class myClass = urlClassLoader.loadClass("TestActionInterface");
            ActionInterface action = (ActionInterface) myClass.newInstance();
            System.out.println(action.action());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
